package com.ml.mall.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.bean.page.Page;
import com.ml.mall.bean.req.sys.SysRoutingQueryPageDto;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.entity.sys.SysRouting;
import com.ml.mall.service.sys.SysRoutingService;
import com.ml.mall.mapper.SysRoutingMapper;
import com.ml.mall.utils.MyBatisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class SysRoutingServiceImpl extends ServiceImpl<SysRoutingMapper, SysRouting>
    implements SysRoutingService{

    @Autowired
    private SysRoutingMapper sysRoutingMapper;

    @Override
    public List<String> getRoutingPermissionsByCompanyId(Long companyId) {
        return sysRoutingMapper.selectByCompanyId(companyId);
    }

    @Override
    public Page<SysRouting> routingQueryPage(SysRoutingQueryPageDto dto) {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<SysRouting> queryPage = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(dto.getPageIndex(), dto.getPageNum());
        LambdaQueryWrapper<SysRouting> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dto.getType() != null, SysRouting::getType, dto.getType())
                    .in(dto.getRoleType() != null, SysRouting::getRoleType, dto.getRoleType(), CommonConsts.ROLE_TYPE_ALL);
        page(queryPage, queryWrapper);

        return MyBatisUtil.parsePageResult(queryPage);
    }

    @Override
    public List<Long> querySubPermissionIds(List<Long> menuIds, Integer roleType) {
        LambdaQueryWrapper<SysRouting> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(SysRouting::getId)
                    .eq(SysRouting::getRoleType, roleType)
                    .in(SysRouting::getParentId, menuIds)
                    .eq(SysRouting::getType, CommonConsts.PERMISSION_TYPE_OF_PERMISSION);
        List<SysRouting> routingList = list(queryWrapper);
        return routingList.stream().map(SysRouting::getId).collect(Collectors.toList());
    }
}




